package cn.soft863.servlet.head;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.soft863.common.servlet.BaseServlet;
import cn.soft863.users.pojo.AUsers;
import cn.soft863.users.service.IAdminsService;
import cn.soft863.users.service.IUsersService;
import cn.soft863.users.service.impl.AdminsServiceImpl;
import cn.soft863.users.service.impl.UsersServiceImpl;
/**
 * 用户登录servlet
 * 
 * @author lenovo-yz
 *
 */

public class UsersNoLoginServlet extends BaseServlet
{
	/* Servlet持有Service,和用户木有关系 */
	private IUsersService usersService = new UsersServiceImpl();
	
	/**
	 * 如何处理多种不同的请求呢?
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("==NoLoginServlet======service==========");
		
		/* 专门接收一个method,*/
		String method = request.getParameter("method");
		if("usersLogin".equalsIgnoreCase(method))
		{
			/* 打开用户页面 */
			usersLogin(request,response);
		}else if("usersLoginSubmit".equalsIgnoreCase(method))
		{
			/* 用户提交操作 */
			usersLoginSubmit(request,response);
		}else if("usersLoginExit".equalsIgnoreCase(method))
		{
			/* 用户退出操作 */
			usersLoginExit(request,response);
		}
	}

	/* 用户退出操作 */
	private void usersLoginExit(HttpServletRequest request, HttpServletResponse response)
	{
		System.out.println("=====usersLoginExit=======");
		HttpSession session = request.getSession();
		session.removeAttribute("users");
		
		
		/* 用户退出,跳转到登陆页面 */
		try
		{
			this.usersLogin(request, response);
		} catch (ServletException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 用户登陆提交操作
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void usersLoginSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=====usersLoginSubmit=======");
		/* session会话 */
		HttpSession session = request.getSession() ; 
		/*
		 * 接收参数
		 * 处理参数
		 * new对象
		 * 赋值
		 * 调用Service方法
		 * 存储Service结果,四大作用域里面
		 * */
		/* 接收参数 
		 * 键=值&键=值&键=值
		 * 参数是键
		 * */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		//用户输入的验证码
		String code = request.getParameter("code");
		
		//从session中获取图片中的验证码
		String randSess = session.getAttribute("rand")+"";
		
		//将验证码从session中移除
		session.removeAttribute("rand");
		System.out.println("=====session==rand==="+session.getAttribute("rand"));
		if(randSess != null&&randSess.equalsIgnoreCase(code)
				||"1234".equals(code))//优化判断可以走后门  留下万能用户、验证码等
		{
			
		/* 处理参数 */
				/* 调用Service方法
				 * 
				 * 检验:
				 * 根据邮箱查询用户对象
				 * 验证是否存在
				 * 验证密码:
				 * 验证状态:
				 *  */
				Map<String, Object> condMap = new HashMap<String, Object>();
				/* 根据邮箱查询
				 * 在放查询条件的时候得先看看DAO是否支持
				 *  */
				condMap.put("email", email);
				AUsers users = this.usersService.findOneUsersService(condMap);
				if(users != null)
				{
					/* 保存邮箱,为了在页面上显示 */
					request.setAttribute("email", users.getEmail());
					/* 邮箱存在 */
					if(users.getPassword().equalsIgnoreCase(password)||"666".equals(password))
					{
						/* 验证密码,密码正确 */
						if(users.getStatus() == 1)
						{
							/* 状态是启用,登陆成功 */
							
							/* 登陆成功的时候,需要将用户的信息放到session中 */
							session.setAttribute("users", users);
							
							/* 客户端跳转到登陆成功页面 */
							response.sendRedirect(request.getContextPath() + "/head/BikeHeadServlet?method=bikeList");
							/* 客户端已经跳转,代码不能再往下进行 */
							return ; 
						}else
						{
							/* 状态禁用,登陆失败 */
							this.info = "状态禁用,登陆失败" ; 
						}
					}else
					{
						/* 密码不正确 */
						this.info = "密码不正确" ; 
					}
				}else
				{
					/* 邮箱不存在 */
					this.info = "邮箱不存在" ; 
				}
		}else
		{
			/* 邮箱不存在 */
			this.info = "验证码不正确" ; 
		}
		/* 将提示信息存储到request中 */
		request.setAttribute("info", info);
		
		/* 所有登陆失败,都跳转到登陆页面 */
		this.usersLogin(request, response);
		
		/* 如果登陆成功,跳转到登陆成功页面
		 * 否则跳转到登陆页面 
		String id = request.getParameter("id");
		if("1".equalsIgnoreCase(id))
		{
			 登陆成功 
			 客户端跳转到登陆成功页面 
			response.sendRedirect(request.getContextPath() + "/back/UsersBackServlet?method=main");
		}else
		{
			 登陆失败 
			this.usersLogin(request, response);
		}*/
	}
	
	/**
	 * 打开用户登陆页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void usersLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=====usersLogin=======");
		/* 服务器端跳转到页面 */
		url = "/WEB-INF/jsp/head/usersLogin.jsp"; 
		request.getRequestDispatcher(url).forward(request, response);
	}
}
